<?php
namespace admin\model;

use admin\model\Model;

class ReplyModel extends Model{
    
    /**
     *  获取所有评论
     */
    public function getReplyAll($params = array('page' => 0,'limit' => 0)){
        $curr = ($params['page']-1)*$params['limit'];
        $sql = 'SELECT comment_reply.id,comment_reply.user_name AS reply_name,comment_reply.user_mail,comment_reply.praise_count,comment_reply.content AS reply_content,comment_reply.time,comment_reply.state,comment.user_name AS comment_name,comment.content AS comment_content,blog_info.title 
        FROM blog_info,COMMENT,comment_reply WHERE blog_info.id = comment.blog_id AND comment.id = comment_reply.comment_id ';
        if(isset($params['content'])){
            $sql.=" AND comment_reply.content LIKE :content ORDER BY comment_reply.id ASC LIMIT {$curr},{$params['limit']} ";
            $data = $this->p_queryAll($sql,array('content'=>'%'.$params['content'].'%'));
        }else{
            $sql.=" ORDER BY comment_reply.id ASC LIMIT {$curr},{$params['limit']} ";
            $data = $this->queryAll($sql);
        }
        return $this->filter($data);      //过滤数据传给前台
    }
    /**
     * 数据过滤
     */
    public function filter($data){
        $len = count($data,COUNT_NORMAL);       //统计数组长度
        for($i=0;$i<$len;++$i){
            $data[$i]['time'] =  date("Y-m-d H:i",$data[$i]['time']);       //发表时间
            $data[$i]['state'] = $data[$i]['state']==='0'?'不可见':'可见'; 
        }
        return $data;
    }
    /**
     * 获取评论数量
     */
    public function getReplyCount($params){
        $sql = 'SELECT COUNT(*) AS count FROM comment_reply';
        if(isset($params['content'])){
            $sql.=' WHERE comment_reply.content LIKE :content';
            $data = $this->p_query($sql,array('content'=>'%'.$params['content'].'%'));
        }else{
            $data = $this->query($sql);
        }
        return $data;
    }    
    /**
    * 更新表中部分字段
    */
   public function updateReply($params){
       $sql = "UPDATE comment_reply SET user_name=:user_name,praise_count=:praise_count,content=:content,state=:state WHERE id=:id ";  
       $this->p_execute($sql,$params);
       $num = $this->db->num;
       if($num == 1 ){
           $res['code'] = '1';
       }else{
           $res['code'] = '0';
       }
       return json_encode($res);
   }
   /**
    * 隐藏留言
    */
   public function hideReply($params){
       $sql = "UPDATE comment_reply SET state = 0 WHERE id = :id";
       $this->p_execute($sql,$params);
       $data = [];
       $data['code'] = $this->db->num;
       return $data;
   }
   /**
    * 删除追评
    */
    public function delReply($params){
        $sql = 'DELETE FROM comment_reply WHERE id=:id';
        $this->p_execute($sql,$params);
        $data = [];
        $data['code'] = $this->db->num;
        return $data;
    }
}